using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
using MobileClient.UIProcessService;

namespace MobileClient
{
	/// <summary>
	/// Summary description for ctlButtonMenu.
	/// </summary>
	public class ctlButtonMenu : Control
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private MobileClient.ImageButton btnOption1;
		private MobileClient.ImageButton btnOption2;
		private MobileClient.ImageButton btnOption3;
		private MobileClient.ImageButton btnOption4;
		private MobileClient.ImageButton btnPrevious;
		private MobileClient.ImageButton btnNext;
		private System.Windows.Forms.Label lblOption1;
		private System.Windows.Forms.Label lblOption2;
		private System.Windows.Forms.Label lblOption3;
		private System.Windows.Forms.Label lblOption4;
		private System.ComponentModel.Container components = null;
		private ArrayList m_arrOptions = null;
		private System.Windows.Forms.ImageList m_lstImages = null;
		private System.Windows.Forms.ContextMenu mnuOption1Context;
		private System.Windows.Forms.MenuItem mnuOption1View;
		private System.Windows.Forms.MenuItem mnuOption1Delete;
		private System.Windows.Forms.ContextMenu mnuOption2Context;
		private System.Windows.Forms.MenuItem mnuOption2View;
		private System.Windows.Forms.MenuItem mnuOption2Delete;
		private System.Windows.Forms.ContextMenu mnuOption3Context;
		private System.Windows.Forms.MenuItem mnuOption3View;
		private System.Windows.Forms.MenuItem mnuOption3Delete;
		private System.Windows.Forms.ContextMenu mnuOption4Context;
		private System.Windows.Forms.MenuItem mnuOption4View;
		private System.Windows.Forms.MenuItem mnuOption4Delete;
		private string m_strSelected;
		private int m_nCurrentDisplayIndex;

		public ctlButtonMenu()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			m_arrOptions = null;
			m_strSelected = String.Empty;
			m_nCurrentDisplayIndex = 0;
		}

		// notify parent that line segment was updated
		public event EventHandler OptionClicked;
		public event EventHandler ViewClicked;
		public event EventHandler DeleteClicked;

		public ArrayList Options
		{
			get { return m_arrOptions; }
			set { m_arrOptions = value; }
		}

		public ImageList Images
		{
			get { return m_lstImages; }
			set { m_lstImages = value; }
		}

		public string SelectedOption
		{
			get { return m_strSelected; }
			set { m_strSelected = value; }
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.mnuOption1Context = new System.Windows.Forms.ContextMenu();
			this.mnuOption1View = new System.Windows.Forms.MenuItem();
			this.mnuOption1Delete = new System.Windows.Forms.MenuItem();
			this.mnuOption2Context = new System.Windows.Forms.ContextMenu();
			this.mnuOption2View = new System.Windows.Forms.MenuItem();
			this.mnuOption2Delete = new System.Windows.Forms.MenuItem();
			this.mnuOption3Context = new System.Windows.Forms.ContextMenu();
			this.mnuOption3View = new System.Windows.Forms.MenuItem();
			this.mnuOption3Delete = new System.Windows.Forms.MenuItem();
			this.mnuOption4Context = new System.Windows.Forms.ContextMenu();
			this.mnuOption4View = new System.Windows.Forms.MenuItem();
			this.mnuOption4Delete = new System.Windows.Forms.MenuItem();
			this.btnOption1 = new MobileClient.ImageButton();
			this.btnOption2 = new MobileClient.ImageButton();
			this.btnOption3 = new MobileClient.ImageButton();
			this.btnOption4 = new MobileClient.ImageButton();
			this.btnPrevious = new MobileClient.ImageButton();
			this.btnNext = new MobileClient.ImageButton();
			this.lblOption1 = new System.Windows.Forms.Label();
			this.lblOption2 = new System.Windows.Forms.Label();
			this.lblOption3 = new System.Windows.Forms.Label();
			this.lblOption4 = new System.Windows.Forms.Label();
			this.m_lstImages = new ImageList();
			// 
			// mnuOption1Context
			// 
			this.mnuOption1Context.MenuItems.Add(this.mnuOption1View);
			this.mnuOption1Context.MenuItems.Add(this.mnuOption1Delete);
			// 
			// mnuOption1View
			// 
			this.mnuOption1View.Text = "View";
			this.mnuOption1View.Click += new System.EventHandler(this.mnuOption1View_Click);
			// 
			// mnuOption1Delete
			// 
			this.mnuOption1Delete.Text = "Delete";
			this.mnuOption1Delete.Click += new System.EventHandler(this.mnuOption1Delete_Click);
			// 
			// mnuOption2Context
			// 
			this.mnuOption2Context.MenuItems.Add(this.mnuOption2View);
			this.mnuOption2Context.MenuItems.Add(this.mnuOption2Delete);
			// 
			// mnuOption2View
			// 
			this.mnuOption2View.Text = "View";
			this.mnuOption2View.Click += new System.EventHandler(this.mnuOption2View_Click);
			// 
			// mnuOption2Delete
			// 
			this.mnuOption2Delete.Text = "Delete";
			this.mnuOption2Delete.Click += new System.EventHandler(this.mnuOption2Delete_Click);
			// 
			// mnuOption3Context
			// 
			this.mnuOption3Context.MenuItems.Add(this.mnuOption3View);
			this.mnuOption3Context.MenuItems.Add(this.mnuOption3Delete);
			// 
			// mnuOption3View
			// 
			this.mnuOption3View.Text = "View";
			this.mnuOption3View.Click += new System.EventHandler(this.mnuOption3View_Click);
			// 
			// mnuOption3Delete
			// 
			this.mnuOption3Delete.Text = "Delete";
			this.mnuOption3Delete.Click += new System.EventHandler(this.mnuOption3Delete_Click);
			// 
			// mnuOption4Context
			// 
			this.mnuOption4Context.MenuItems.Add(this.mnuOption4View);
			this.mnuOption4Context.MenuItems.Add(this.mnuOption4Delete);
			// 
			// mnuOption4View
			// 
			this.mnuOption4View.Text = "View";
			this.mnuOption4View.Click += new System.EventHandler(this.mnuOption4View_Click);
			// 
			// mnuOption4Delete
			// 
			this.mnuOption4Delete.Text = "Delete";
			this.mnuOption4Delete.Click += new System.EventHandler(this.mnuOption4Delete_Click);
			//
			// m_lstImages
			//
			m_lstImages.ImageSize = new Size(64, 64);
			m_lstImages.Images.Add(new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.Forms.gif")));
            m_lstImages.Images.Add(new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.Configure.gif")));
            m_lstImages.Images.Add(new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.Download.gif")));
            m_lstImages.Images.Add(new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.Upload.gif")));
			// 
			// btnOption1
			// 
			this.btnOption1.BackColor = System.Drawing.Color.White;
			this.btnOption1.ImageButtonDown = null;
			this.btnOption1.ImageButtonUp = null;
			this.btnOption1.Pressed = false;
			this.btnOption1.Location = new System.Drawing.Point(26, 8);
			this.btnOption1.Size = new System.Drawing.Size(64, 64);
			this.btnOption1.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnOption1.Click += new System.EventHandler(this.btnOption1_Click);
			this.btnOption1.ContextMenu = mnuOption1Context;
			// 
			// lblOption1
			// 
			this.lblOption1.Location = new System.Drawing.Point(10, 76);
			this.lblOption1.Size = new System.Drawing.Size(96, 32);
			this.lblOption1.Text = "Option 1";
			this.lblOption1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// btnOption2
			// 
			this.btnOption2.BackColor = System.Drawing.Color.White;
			this.btnOption2.ImageButtonDown = null;
			this.btnOption2.ImageButtonUp = null;
			this.btnOption2.Location = new System.Drawing.Point(142, 8);
			this.btnOption2.Pressed = false;
			this.btnOption2.Size = new System.Drawing.Size(64, 64);
			this.btnOption2.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnOption2.Click += new System.EventHandler(this.btnOption2_Click);
			this.btnOption2.ContextMenu = mnuOption2Context;
			// 
			// lblOption2
			// 
			this.lblOption2.Location = new System.Drawing.Point(126, 76);
			this.lblOption2.Size = new System.Drawing.Size(96, 32);
			this.lblOption2.Text = "Option 2";
			this.lblOption2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// btnOption3
			// 
			this.btnOption3.BackColor = System.Drawing.Color.White;
			this.btnOption3.ImageButtonDown = null;
			this.btnOption3.ImageButtonUp = null;
			this.btnOption3.Location = new System.Drawing.Point(26, 110);
			this.btnOption3.Pressed = false;
			this.btnOption3.Size = new System.Drawing.Size(64, 64);
			this.btnOption3.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnOption3.Click += new System.EventHandler(this.btnOption3_Click);
			this.btnOption3.ContextMenu = mnuOption3Context;
			// 
			// lblOption3
			// 
			this.lblOption3.Location = new System.Drawing.Point(10, 180);
			this.lblOption3.Size = new System.Drawing.Size(96, 32);
			this.lblOption3.Text = "Option 3";
			this.lblOption3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// btnOption4
			// 
			this.btnOption4.BackColor = System.Drawing.Color.White;
			this.btnOption4.ImageButtonDown = null;
			this.btnOption4.ImageButtonUp = null;
			this.btnOption4.Location = new System.Drawing.Point(142, 110);
			this.btnOption4.Pressed = false;
			this.btnOption4.Size = new System.Drawing.Size(64, 64);
			this.btnOption4.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnOption4.Click += new System.EventHandler(this.btnOption4_Click);
			this.btnOption4.ContextMenu = mnuOption4Context;
			// 
			// lblOption4
			// 
			this.lblOption4.Location = new System.Drawing.Point(126, 180);
			this.lblOption4.Size = new System.Drawing.Size(96, 32);
			this.lblOption4.Text = "Option 3";
			this.lblOption4.TextAlign = System.Drawing.ContentAlignment.TopCenter;
			// 
			// btnPrevious
			// 
			this.btnPrevious.BackColor = System.Drawing.Color.White;
			this.btnPrevious.ImageButtonUp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.up_prev.gif"));
			this.btnPrevious.ImageButtonDown = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.down_prev.gif"));
			this.btnPrevious.Location = new System.Drawing.Point(23, 220);
			this.btnPrevious.Pressed = false;
			this.btnPrevious.Size = new System.Drawing.Size(72, 20);
			this.btnPrevious.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			// 
			// btnNext
			// 
			this.btnNext.BackColor = System.Drawing.Color.White;
			this.btnNext.ImageButtonUp = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.up_next.gif"));
			this.btnNext.ImageButtonDown = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MobileClient.Graphics.down_next.gif"));
			this.btnNext.Location = new System.Drawing.Point(139, 220);
			this.btnNext.Pressed = false;
			this.btnNext.Size = new System.Drawing.Size(72, 20);
			this.btnNext.Type = MobileClient.ImageButton.ButtonType.Button;
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// ctlButtonMenu
			// 
			this.Size = new System.Drawing.Size(232, 270);
			this.Controls.Add(this.btnOption1);
			this.Controls.Add(this.lblOption1);
			this.Controls.Add(this.btnOption2);
			this.Controls.Add(this.lblOption2);
			this.Controls.Add(this.btnOption3);
			this.Controls.Add(this.lblOption3);
			this.Controls.Add(this.btnOption4);
			this.Controls.Add(this.lblOption4);
			this.Controls.Add(this.btnPrevious);
			this.Controls.Add(this.btnNext);

		}
		#endregion

		public void InitDisplay()
		{
			m_nCurrentDisplayIndex = 0;
			UpdateButtons();
		}

		private void btnOption1_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption1.Text;
			RaiseOptionClickedEvent();
		}

		private void btnOption2_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption2.Text;
			RaiseOptionClickedEvent();
		}

		private void btnOption3_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption3.Text;
			RaiseOptionClickedEvent();
		}

		private void btnOption4_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption4.Text;
			RaiseOptionClickedEvent();
		}

		private void btnPrevious_Click(object sender, System.EventArgs e)
		{
			m_nCurrentDisplayIndex -= 4;
			if(m_nCurrentDisplayIndex < 0)
			{
				m_nCurrentDisplayIndex = 0;
			}
			UpdateButtons();
		}

		private void btnNext_Click(object sender, System.EventArgs e)
		{
			m_nCurrentDisplayIndex += 4;
			if(m_nCurrentDisplayIndex > m_arrOptions.Count)
			{
				m_nCurrentDisplayIndex = (m_arrOptions.Count - 4);
				if(m_nCurrentDisplayIndex < 0)
				{
					m_nCurrentDisplayIndex = 0;
				}

			}
			UpdateButtons();
		}

		/// <summary>
		/// Notify container that a option has been clicked.
		/// </summary>
		private void RaiseOptionClickedEvent()
		{
			if (this.OptionClicked != null)
			{
				OptionClicked(this, EventArgs.Empty);
			}
		}

		/// <summary>
		/// Notify container that a option has been clicked.
		/// </summary>
		private void RaiseViewClickedEvent()
		{
			if (this.ViewClicked != null)
			{
				ViewClicked(this, EventArgs.Empty);
			}
		}

		/// <summary>
		/// Notify container that a option has been clicked.
		/// </summary>
		private void RaiseDeleteClickedEvent()
		{
			if (this.DeleteClicked != null)
			{
				DeleteClicked(this, EventArgs.Empty);
			}
		}

		private void UpdateButtons()
		{
			clsButtonMenuItem objItem;
			Image objImage = null;

			lblOption1.Visible = false;
			btnOption1.Visible = false;
			lblOption2.Visible = false;
			btnOption2.Visible = false;
			lblOption3.Visible = false;
			btnOption3.Visible = false;
			lblOption4.Visible = false;
			btnOption4.Visible = false;


			for(int nIndex = 0; nIndex < 4; nIndex++)
			{
				if((m_nCurrentDisplayIndex + nIndex) < m_arrOptions.Count)
				{
					objItem = (clsButtonMenuItem)m_arrOptions[m_nCurrentDisplayIndex + nIndex];
					switch(objItem.Type)
					{
						case clsButtonMenuItem.ItemType.Template:
							objImage = m_lstImages.Images[0];
							break;
						case clsButtonMenuItem.ItemType.Configuration:
							objImage = m_lstImages.Images[1];
							break;
						case clsButtonMenuItem.ItemType.Download:
							objImage = m_lstImages.Images[2];
							break;
						case clsButtonMenuItem.ItemType.Upload:
							objImage = m_lstImages.Images[3];
							break;
					}

					switch(nIndex)
					{
						case 0:
							lblOption1.Text = objItem.Name;
							btnOption1.ImageButtonDown = objImage;
							btnOption1.ImageButtonUp = objImage;
							lblOption1.Visible = true;
							btnOption1.Visible = true;
							break;					
						case 1:
							lblOption2.Text = objItem.Name;
							btnOption2.ImageButtonDown = objImage;
							btnOption2.ImageButtonUp = objImage;
							lblOption2.Visible = true;
							btnOption2.Visible = true;
							break;					
						case 2:
							lblOption3.Text = objItem.Name;
							btnOption3.ImageButtonDown = objImage;
							btnOption3.ImageButtonUp = objImage;
							lblOption3.Visible = true;
							btnOption3.Visible = true;
							break;					
						case 3:
							lblOption4.Text = objItem.Name;
							btnOption4.ImageButtonDown = objImage;
							btnOption4.ImageButtonUp = objImage;
							lblOption4.Visible = true;
							btnOption4.Visible = true;
							break;					
					}
				}
			}

			if(m_nCurrentDisplayIndex == 0)
			{
				btnPrevious.Visible = false;
				btnNext.Visible = true;
			}
			else if(m_nCurrentDisplayIndex >= (m_arrOptions.Count - 4))
			{
				btnPrevious.Visible = true;
				btnNext.Visible = false;
			}
			else
			{
				btnPrevious.Visible = true;
				btnNext.Visible = true;
			}

			if(m_arrOptions.Count < 4)
			{
				btnNext.Visible = false;
			}
		}

		private void mnuOption1View_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption1.Text;
			RaiseViewClickedEvent();
		}

		private void mnuOption1Delete_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption1.Text;
			RaiseDeleteClickedEvent();
		}

		private void mnuOption2View_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption2.Text;
			RaiseViewClickedEvent();
		}

		private void mnuOption2Delete_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption2.Text;
			RaiseDeleteClickedEvent();
		}

		private void mnuOption3View_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption3.Text;
			RaiseViewClickedEvent();
		}

		private void mnuOption3Delete_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption3.Text;
			RaiseDeleteClickedEvent();
		}

		private void mnuOption4View_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption4.Text;
			RaiseViewClickedEvent();
		}

		private void mnuOption4Delete_Click(object sender, System.EventArgs e)
		{
			m_strSelected = lblOption4.Text;
			RaiseDeleteClickedEvent();
		}
	}
}
